import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../style/index.dart';
import 'o2_elevated_button.dart';

class O2OverlayEntryDialog {
  factory O2OverlayEntryDialog() => _instance;
  static O2OverlayEntryDialog get instance => _instance;

  static final O2OverlayEntryDialog _instance =
      O2OverlayEntryDialog._internal();
  O2OverlayEntryDialog._internal();

  OverlayEntry? _overlayEntry;

  /// 考勤 极速打卡 弹出提示
  void openFastCheckInDialog(String title, String content) {
    final context = Get.context;
    if (context == null) {
      return;
    }
    _openDialog(context, _buildFastCheckInDialog(context, title, content));
  }

  void _openDialog(BuildContext context, Widget dialog) {
    _overlayEntry = OverlayEntry(builder: ((context) => dialog));
    //插入到 Overlay中显示 OverlayEntry
    Overlay.of(context).insert(_overlayEntry!);
  }

  void _closeDiloag() {
    if (_overlayEntry != null) {
      _overlayEntry!.remove();
      _overlayEntry = null;
    }
  }

  /// 考勤打卡提示的 dialog
  Widget _buildFastCheckInDialog(
      BuildContext context, String title, String content) {
    return _dialogBox(
      Card(
        shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(Radius.circular(10))),
        child: Padding(
            padding:
                const EdgeInsets.only(left: 14, right: 14, bottom: 14),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  children: [
                    const Icon(O2IconFont.ok ,size: 24, color: Colors.green),
                    const Spacer(),
                    IconButton(
                        onPressed: () => _closeDiloag(),
                        icon: const Icon(Icons.close))
                  ],
                ),
                Text(title,
                    style: Theme.of(context)
                        .textTheme
                        .bodyLarge
                        ?.copyWith(fontWeight: FontWeight.bold)),
                const SizedBox(height: 10),
                Expanded(
                    flex: 1,
                    child: Padding(
                        padding:
                            const EdgeInsets.only(left: 10, right: 10),
                        child: Text(content,
                            style: Theme.of(context).textTheme.bodyMedium))),
                const SizedBox(height: 20),
                SizedBox(
                    width: double.infinity,
                    height: 36,
                    child: O2ElevatedButton(() {
                      _closeDiloag();
                    },
                        Text(
                          'positive'.tr,
                          style: const TextStyle(fontSize: 18),
                        ))),
              ],
            )),
      ),
    );
  }

  Widget _dialogBox(Widget child) {
    return Stack(
      children: [
        Container(
          width: double.infinity,
          height: double.infinity,
          color: Colors.black.withOpacity(0.6),
        ),
        Positioned(
            top: 84.h, left: 14.w, right: 14.w, height: 250.h, child: child)
      ],
    );
  }
}
